/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

// eslint-disable-next-line import/no-extraneous-dependencies
import { Request, Response } from 'express';
import { responseSuccess, ResponseItem } from './utils';
import type { TableListItem, TableListParams } from '@/pages/Inn/types/NoticeVerify';
import dayjs from 'dayjs';

function getList(req: Request, res: Response, u: string, tableList: Array<any>) {
  let realUrl = u;
  if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {
    realUrl = req.url;
  }
  const { page = 1, pageSize = 10 } = req.body;
  const params = (req.body as unknown) as TableListParams;

  let dataSource = [...tableList].slice(
    ((page as number) - 1) * (pageSize as number),
    (page as number) * (pageSize as number),
  );

  const result: ResponseItem = {
    code: 0,
    msg: '',
    data: {
      list: dataSource,
      total: tableList.length.toString(),
      pageSize,
      page: parseInt(`${params.page}`, 10).toString() || '1',
    },
  };

  return res.json(result);
}
const generateTodoList = (page: number, pageSize: number) => {
  const tableList: TableListItem[] = [];
  for (let i = 0; i < pageSize; i += 1) {
    const index = (page - 1) * 10 + i;
    tableList.push({
      id: `${index}`,
      phone: `1527777888${index}`,
      league_name: `中国邮政总公司${index}`,
      title: `${index}找不到快递`,
      status: '2',
      created_time: `创建——${dayjs().format('YYYY-MM-DDTHH:mm:ss')}`,
      update_time: `${dayjs().format('YYYY-MM-DDTHH:mm:ss')}`,
      content: `${index}内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容`,
    });
  }
  return tableList;
};
const generatePrevOrNextNotice = (req: Request, res: Response) => {
  const { id }: { id: number } = req.body;
  const result: ResponseItem = {
    code: 0,
    msg: 'success',
    data: {
      id: '3',
      league_id: '12',
      status: '2',
      is_deleted: '0',
      created_time: '2021-02-07 16:00:29',
      update_time: '2021-02-07 16:00:29',
      title: `${id}下一条审核的标题`,
      content: `${id}下一条审核的内容`,
      front_id: Number(id) + 1,
      last_id: Number(id) - 1 < 0 ? '' : Number(id) - 1,
    },
  };
  return res.json(result);
};
const todoList = generateTodoList(1, 200);

export default {
  'POST /Inn/getLeagueNoticeList': (req: Request, res: Response, u: string) =>
    getList(req, res, u, todoList),
  'POST /Inn/updateStatus': (req: Request, res: Response) => responseSuccess(req, res),
  'POST /Inn/noticeDetail': (req: Request, res: Response) => generatePrevOrNextNotice(req, res),
};
